import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class FLogin {
    private JPanel rootPanel;
    private JTable table1;

    private JMenuBar jMenuBar;

    public FLogin() {
        //跳转到FLogin窗体
        JFrame jFrame = new JFrame("zhuren ");
        jFrame.setSize(500,400);
        jFrame.setContentPane(rootPanel);
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        initMenuBar(jFrame);
        initData();
    }

    private void initData() {
        //使用"SELECT * FROM book"查询"book"表中的所有数据
        Connection connection = JdbcHelper.getConnection();
        final Object[] columnNames = {"编号", "书名", "价格"};

        Object[][] rowData = {
                {"ddd", "男", "江苏南京", "1378313210", "03/24/1985", "学生", "寄生中", "未婚", "没"},
                {"eee", "女", "江苏南京", "13645181705", "xx/xx/1985", "家教", "未知", "未婚", "好象没"},
                {"fff", "男", "江苏南京", "13585331486", "12/08/1985", "汽车推销员", "不确定", "未婚", "有"},
                {"ggg", "女", "江苏南京", "81513779", "xx/xx/1986", "宾馆服务员", "确定但未知", "未婚", "有"},
                {"hhh", "男", "江苏南京", "13651545936", "xx/xx/1985", "学生", "流放中", "未婚", "无数次分手后没有"}
        };
        Object obj = "Hello";


//         TableColumn column = new TableColumn();
//         column.setHeaderValue(columnNames);
//
//        table1.addColumn(column);
//        scroll.setViewportView(table1);

        DefaultTableModel dataModel = new DefaultTableModel(columnNames,0);

        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from book");
            while (resultSet.next()) {
                //将数据显示在jtable中
                dataModel.addRow(new Object[]{resultSet.getInt("id"),
                        resultSet.getString("name"), resultSet.getInt("price")});
            }
            JdbcHelper.closeConnection(connection, statement, resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        table1.setModel(dataModel);


    }

    private void initMenuBar(JFrame jFrame) {
        jMenuBar = new JMenuBar();
        JMenu menuFile = new JMenu("File");
        JMenu menuEdit = new JMenu("Edit");
        jMenuBar.add(menuFile);
        jMenuBar.add(menuEdit);
        JMenuItem itemNew = new JMenuItem("New");
        JMenuItem itemOpen = new JMenuItem("Open");
        menuFile.add(itemNew);
        menuFile.add(itemOpen);
        jFrame.setJMenuBar(jMenuBar);
    }

    public JPanel getRootPanel() {
        return rootPanel;
    }

    public void setRootPanel(JPanel rootPanel) {
        this.rootPanel = rootPanel;
    }
}
